package com.fishery.base.controller;

import com.fishery.base.entity.GermchitType;
import com.fishery.base.service.GermchitTypeService;
import com.fishery.entity.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;


@RestController
@RequestMapping("/base/germchit/type")
@Api(tags = "种苗种类养殖环境信息管理模块")
@CrossOrigin
public class GermchitTypeController {

	@Autowired
	private GermchitTypeService germchitTypeService;

	@GetMapping
	@ApiOperation("查询所有类型种苗的养殖环境信息")
	public Result searchAllGermchitType() {
		return Result.success().data(germchitTypeService.list(null));
	}

	@GetMapping("/{id}")
	@ApiOperation("查询某种种苗的养殖环境信息")
	public Result searchGermchitTypeById(@PathVariable(name = "id", required = true)
										 @ApiParam(value = "id", required = true) String id) {
		if (StringUtils.isEmpty(id)) {
			return Result.error("查询失败");
		}
		return Result.success().data(germchitTypeService.getById(id));
	}

	@PostMapping
	@ApiOperation("添加种苗类型养殖环境信息")
	public Result addGermchitType(@RequestBody @ApiParam(value = "germchitType") GermchitType germchitType) {
		if (germchitType == null
				|| StringUtils.isEmpty(germchitType.getName())
				|| StringUtils.isEmpty(germchitType.getAmmoniaValue())
				|| StringUtils.isEmpty(germchitType.getDissolvedOxygenValue())
				|| StringUtils.isEmpty(germchitType.getMaxDensity())
				|| StringUtils.isEmpty(germchitType.getMinDensity())
				|| StringUtils.isEmpty(germchitType.getPh())
				|| StringUtils.isEmpty(germchitType.getSalinity())
				|| StringUtils.isEmpty(germchitType.getWaterTemperature())) {
			return Result.error("添加失败");
		}
		boolean ret = germchitTypeService.save(germchitType);
		return ret ? Result.success("添加成功") : Result.error("添加失败");
	}

	@PostMapping("/excel")
	@ApiOperation("将Excel文件导入数据保存到数据库中")
	public Result addGermchitTypeByExcel(MultipartFile file) {
		boolean ret = germchitTypeService.saveDataToDBWithExcel(file);
		return ret ? Result.success("导入成功") : Result.error("导入失败");
	}

	@DeleteMapping("/{id}")
	@ApiOperation("删除种苗类型养殖环境信息")
	public Result deleteGermchitType(@PathVariable(name = "id", required = true)
									 @ApiParam(value = "id", required = true) String id) {
		if (StringUtils.isEmpty(id)) {
			return Result.error("删除失败");
		}
		boolean ret = germchitTypeService.removeById(id);
		return ret ? Result.success("删除成功") : Result.error("删除失败");
	}

	@GetMapping("/excel")
	@ApiOperation("以excel下载种苗养殖环境说明书")
	public void getGermchitTypeByExcel(HttpServletResponse response) {
		germchitTypeService.downloadData(response);
	}

}
